package lovebomb.jfilesearch.engine.filetype;

import java.io.FileInputStream;

import org.apache.poi.hslf.extractor.PowerPointExtractor;
import org.apache.poi.hssf.extractor.ExcelExtractor;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xslf.XSLFSlideShow;
import org.apache.poi.xslf.extractor.XSLFPowerPointExtractor;
import org.apache.poi.xssf.extractor.XSSFExcelExtractor;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

public class MSOfficeRead {
	private static MSOfficeRead officeData = null;
	
	private MSOfficeRead(){
		
	}
	
	public static MSOfficeRead getInstance(){
		if( officeData == null){
			officeData = new MSOfficeRead();
		}		
		return officeData;
	}
	
	@SuppressWarnings("finally")
	public String readWordFile(String strFile){
	   POIFSFileSystem fs = null;
       String strNote = null;
        
        try {         
            if( strFile.endsWith(".doc")){
            	fs = new POIFSFileSystem(new FileInputStream(strFile));
            	HWPFDocument doc = new HWPFDocument(fs);
	            WordExtractor we = new WordExtractor(doc);  
	            strNote = we.getText();	
            }
            else if( strFile.endsWith(".docx")){
            	XWPFDocument doc = new XWPFDocument(new FileInputStream(strFile));
            	XWPFWordExtractor we = new XWPFWordExtractor(doc);
            	strNote = we.getText();	            	
            }
            
        } catch (Exception e) {
        } finally {
            return strNote;
        }
	}
	
	@SuppressWarnings("finally")
	public String readExcelFile( String strFile){
		   POIFSFileSystem fs = null;
	       String strNote = null;
	        
	        try {         
	            if( strFile.endsWith(".xls")){
	            	fs = new POIFSFileSystem(new FileInputStream(strFile));
	            	ExcelExtractor ee = new ExcelExtractor(fs);
		            strNote = ee.getText();	
	            }
	            else if( strFile.endsWith(".xlsx")){
	            	XSSFExcelExtractor ee = new XSSFExcelExtractor( strFile);
	            	strNote = ee.getText();	            	
	            }
	            
	        } catch (Exception e) {
	        } finally {
	            return strNote;
	        }		
		}
	
	public String readPowerPointFile( String strFile){
		   POIFSFileSystem fs = null;
	       String strNote = null;
	        
	        try {         
	            if( strFile.endsWith(".ppt")){
	            	fs = new POIFSFileSystem(new FileInputStream(strFile));
	            	PowerPointExtractor ee = new PowerPointExtractor(fs);
		            strNote = ee.getText();	
	            }
	            else if( strFile.endsWith(".pptx")){
	            	XSLFSlideShow ss = new XSLFSlideShow( strFile);
	            	XSLFPowerPointExtractor ee = new XSLFPowerPointExtractor(ss);
	            	strNote = ee.getText();	            	
	            }
	            
	        } catch (Exception e) {
	        } finally {
	            return strNote;
	        }		
		}
		
}
